package com.foeye.jaxrs2.client;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;

public class JaxClient {

	private static final String REST_SERVICE_URL = "http://localhost:8080/resteasy";

	private static final String TITLE = "One big book";

	// private static final BigDecimal PRICE = new BigDecimal("20.0");
	// private static final GregorianCalendar PUBLISHED = new
	// GregorianCalendar(2013, 12, 24);

	public static void main(String[] args) {

//		getAllUser();
		addUser();
//		getUser();
		
	}
	
	public static void getUser() {
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		String urllistallbook = REST_SERVICE_URL + "/user/get?id=123";
		
		HttpGet httpGet = new HttpGet(urllistallbook);
		try {
			HttpResponse response = httpClient.execute(httpGet);
			int code = response.getStatusLine().getStatusCode();
			// 204服务器已经处理成功，但没有返回任何数据
			if (code == 200 || code == 204) {
				String content = EntityUtils.toString(response.getEntity());
				System.out.println("输出===>" + content);
			} else {
				System.err.println("建立请求失败，返回状态码==》" + code);
			}

		} catch (Exception e) {
		} finally {
			httpGet.releaseConnection();
		}
	}

	public static void addUser() {
		User user = new User();
		user.setId("112");
		user.setName("二蛋儿");
		user.setAge(22);
		
		CloseableHttpClient httpclient = HttpClientBuilder.create().build();
		String url = REST_SERVICE_URL + "/user/add";
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		String userjson = JSON.toJSONString(user);
		System.out.println("userjson = " + userjson);
		params.add(new BasicNameValuePair("user", userjson));

		HttpPost httppost = new HttpPost(url);
		

		try {
			httppost.setHeader("Content-Type", "application/json;charset=utf-8");
			httppost.setHeader("Accept", "application/json;charset=utf-8");
			httppost.setEntity(new UrlEncodedFormEntity(params));
			
			CloseableHttpResponse response = httpclient.execute(httppost);
			System.out.println(response.toString());

			HttpEntity entity = response.getEntity();
			String jsonStr = EntityUtils.toString(entity, "utf-8");
			System.out.println(jsonStr);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		httppost.releaseConnection();
	}

	public static void getAllUser() {
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		String urllistallbook = REST_SERVICE_URL + "/user";

		HttpGet httpGet = new HttpGet(urllistallbook);
		try {
			HttpResponse response = httpClient.execute(httpGet);
			int code = response.getStatusLine().getStatusCode();
			// 204服务器已经处理成功，但没有返回任何数据
			if (code == 200 || code == 204) {
				String content = EntityUtils.toString(response.getEntity());
				System.out.println("listaoobook输出===>" + content);
			} else {
				System.err.println("建立请求失败，返回状态码==》" + code);
			}

		} catch (Exception e) {
		} finally {
			httpGet.releaseConnection();
		}
	}

}
